package zj.xinxin.web.controller.security;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import zj.xinxin.api.moudle.security.api.RoleService;
import zj.xinxin.api.moudle.security.dto.ConsumerInfoDto;
import zj.xinxin.api.moudle.security.dto.RoleDto;
import zj.xinxin.api.moudle.security.vo.RoleCreateVo;
import zj.xinxin.api.moudle.security.vo.RoleInfoSearchVo;
import zj.xinxin.api.moudle.security.vo.RoleUpdateVo;
import zj.xinxin.common.annotation.Log;
import zj.xinxin.common.constant.LogConst;
import zj.xinxin.common.result.ResponseResult;

import javax.validation.Valid;
import java.util.List;

/**
 * @author junzhou
 * @date 2022/3/23 16:30
 * @since 1.8
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色信息")
public class RoleController {

    @Autowired
    RoleService roleService;

    @ApiOperation(value = "查询当前用户所能查看的角色信息")
    @Log(logType = LogConst.LOG_TYPE_QUERY)
    @GetMapping("/list")
    public ResponseResult<List<RoleDto>> list() {
        List<RoleDto> roleList = roleService.getRoleList();
        return ResponseResult.success(roleList);
    }


    @ApiOperation(value = "查询用户信息")
    @Log(logType = LogConst.LOG_TYPE_QUERY)
    @GetMapping("/search")
    public ResponseResult<List<RoleDto>> searchUserInfo(RoleInfoSearchVo searchVo) {
        List<RoleDto> result = roleService.searchByCondition(searchVo);
        return ResponseResult.success("查询成功！", result);
    }

    @ApiOperation(value = "添加角色信息")
    @Log(logType = LogConst.LOG_TYPE_SAVE)
    @PostMapping("/add")
    public ResponseResult<Boolean> add(@RequestBody @Valid RoleCreateVo roleInfo){
        Boolean res = roleService.addRole(roleInfo);

        return res ? ResponseResult.success("添加角色成功！", true) :
                ResponseResult.fail("添加角色失败！", false);
    }

    @ApiOperation(value = "更新角色信息")
    @Log(logType = LogConst.LOG_TYPE_UPDATE)
    @PutMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody @Valid RoleUpdateVo roleInfo){

        Boolean res = roleService.updateRole(roleInfo);

        return res ? ResponseResult.success("更新角色成功！", true) :
                ResponseResult.fail("更新角色失败！", false);
    }


    @ApiOperation(value = "删除角色信息")
    @Log(logType = LogConst.LOG_TYPE_DELETE)
    @DeleteMapping("/delete/{roleId}")
    public ResponseResult<Boolean> delete(@PathVariable("roleId") String roleId){
        boolean res = roleService.removeById(roleId);

        return res ? ResponseResult.success("删除角色成功！", true) :
                ResponseResult.fail("删除角色失败！", false);
    }

}
